package com.gxa.portal.controller;

import com.gxa.cart.service.CartService;
import com.gxa.common.exception.BusinessException;
import com.gxa.common.result.Result;
import com.gxa.portal.interceptor.UserThreadLocal;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

/**
 * Created by zxd on 2021/1/7
 **/
@RequestMapping("/cart")
@Controller
@ResponseBody
public class CartController {

    @Reference
    private CartService cartService;


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result addGoodsToCart(String skuId, Integer num) throws BusinessException {

        //使用dubbo隐式传参后此处第一个参数可以省略
        cartService.addGoodsToCart(UserThreadLocal.get().getId(),skuId,num);
        return new Result();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateCartItemNum(String skuId,Integer num){
        cartService.updateCartItemNum(UserThreadLocal.get().getId(),skuId,num);
        return new Result();
    }

    @RequestMapping(value = "/removeCartItem",method = RequestMethod.POST)
    public Result removeCartItem(String skuId){
        cartService.removeCartItem(UserThreadLocal.get().getId(),skuId);
        return new Result();
    }

    @RequestMapping(value = "/clearCart",method = RequestMethod.POST)
    public Result clearCart(){
        cartService.clearCart(UserThreadLocal.get().getId());
        return new Result();
    }

    @PostMapping("/queryUserCart")
    public Result queryUserCart(){
        Map result = cartService.queryUserCart(UserThreadLocal.get().getId());
        return new Result(result);
    }

}
